package com.doctor.adapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.jar.Attributes.Name;

import com.doctor.bean.ContactsBean;
import com.doctor.ui.R;

import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

public class MultipleDialogAdapter extends BaseAdapter{

	/**
	 * 多选对话框adapter
	 */
	
	private Context context;
	private List<ContactsBean> beanList;
	private Map<Integer, ContactsBean> map;
	
	public MultipleDialogAdapter(Context context,List<ContactsBean> beanList){
		this.context=context;
		this.beanList=beanList;
		map=new HashMap<Integer, ContactsBean>();
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return beanList.size();
	}

	@Override
	public ContactsBean getItem(int arg0) {
		// TODO Auto-generated method stub
		return beanList.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}
	
	public void changeChecked(ContactsBean bean){
		int id=bean.getId();
		if (map.containsKey(id)) {
			map.remove(id);
		} else {
			map.put(id, bean);
		}
		this.notifyDataSetChanged();
	}
	
	public Map<Integer, ContactsBean> getAllSelected(){
		return map;
	}

	@Override
	public View getView(int arg0, View arg1, ViewGroup arg2) {
		// TODO Auto-generated method stub
		ViewHolder holder;
		if (arg1==null) {
			holder=new ViewHolder();
			arg1=View.inflate(context, R.layout.multiple_dialog_item, null);
			holder.name=(TextView) arg1.findViewById(R.id.name);
			holder.tel=(TextView) arg1.findViewById(R.id.tel);
			holder.img=(ImageView) arg1.findViewById(R.id.checkBox);
			
			arg1.setTag(holder);
		} else {
			holder=(ViewHolder) arg1.getTag();
		}
		
		ContactsBean bean=beanList.get(arg0);
		
		holder.name.setText(bean.getName());
		holder.tel.setText(bean.getContacts());
		if (map.containsKey(bean.getId())) {
			holder.img.setImageResource(R.drawable.selected_checkbox);
		} else {
			holder.img.setImageResource(R.drawable.unselected_checkbox);
		}
		
		arg1.setTag(R.id.bean_tag,bean);
		return arg1;
	}
	
	private class ViewHolder{
		TextView name;
		TextView tel;
		ImageView img;
	}
	
	
	
}
